package com.bei_en.teacher.admin.controller.feedback;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.feedback.bean.Feedback;
import com.bei_en.teacher.admin.model.feedback.condition.AddFeedbackCondition;
import com.bei_en.teacher.admin.model.feedback.condition.FeedbackCondition;
import com.bei_en.teacher.admin.model.feedback.vo.FeedbackVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.service.feedback.FeedbackService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-11-26 10:09:53
 */
@Controller
@RequestMapping("/api/Feedback")
public class FeedbackAPPController extends BaseController {

	@Autowired
	FeedbackService feedbackService;

	@RequestMapping("/feedback")
	@ResponseBody
	public JsonResult feedback(Model model, @Valid FeedbackCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {

			List<FeedbackVo> list = new ArrayList<FeedbackVo>();
			list = feedbackService.findFeedbackPage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			FeedbackVo feedbackVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				feedbackVo = list.get(i);
				seetingJsonObject(object, "id", feedbackVo.getId());
				seetingJsonObject(object, "content", feedbackVo.getContent());
				seetingJsonObject(object, "updateId", feedbackVo.getUpdateId());
				seetingJsonObject(object, "userId", feedbackVo.getUserId());
				seetingJsonObject(object, "teacher", feedbackVo.getteacher());
				seetingJsonObject(object, "status", feedbackVo.getStatus());
				seetingJsonObject(object, "createTime",
						feedbackVo.getCreateTime());
				seetingJsonObject(object, "updateTime",
						feedbackVo.getUpdateTime());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/feedbackAdd")
	@ResponseBody
	@OAuthRequired
	public JsonResult feedbackAdd(Model model, @Valid AddFeedbackCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {

			UsersVo usersVo = getUser(session);
			FeedbackVo feedback = new FeedbackVo();

			// feedback.setId(vo.getId());
			feedback.setContent(vo.getContent());
			// feedback.setUpdateId(vo.getUpdateId());
			feedback.setUserId(usersVo.getUserId());
			feedback.setStatus(1);
			feedback.setCreateTime(new Date());
			feedback.setUpdateTime(new Date());

			feedbackService.insert(feedback);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/feedbackEdit")
	public JsonResult feedbackEdit(Model model, @Valid FeedbackCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			FeedbackVo feedback = new FeedbackVo();

			feedbackService.updateSelective(feedback);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
